#ifndef MYHELPER_H
#define MYHELPER_H

#include <QtCore>
#include <QtGui>
#include <QDesktopWidget>
#include <iostream>
//#include "frmmessagebox.h"
//#include <Parameter.h>
#include <QApplication>
#include <fstream>
//#include "SolverUtil.h"
//#include <frmmain.h>
//#include "widgetholder.h"

class StyleHelper: public QObject
{

private:
    static std::string s_setup_path;


public:

    //设置为开机启动
    static void AutoRunWithSystem(bool IsAutoRun, QString AppName, QString AppPath)
    {
        QSettings *reg = new QSettings(
            "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
            QSettings::NativeFormat);

        if (IsAutoRun) {
            reg->setValue(AppName, AppPath);
        } else {
            reg->setValue(AppName, "");
        }
    }

    //设置编码为UTF8
    static void SetUTF8Code()
    {
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
        QTextCodec *codec = QTextCodec::codecForName("UTF-8");
        QTextCodec::setCodecForLocale(codec);
        QTextCodec::setCodecForCStrings(codec);
        QTextCodec::setCodecForTr(codec);
#endif
    }

    //设置皮肤样式
    static void SetStyle(const QString &styleName)
    {
#ifdef WIN32
        QFile file(QString("D:\\GitProject\\STK\\resource\\image\\%1.css").arg(styleName));
#else
#endif
        file.open(QFile::ReadOnly);

        QString qss = QLatin1String(file.readAll());
        qApp->setStyleSheet(qss);
        qApp->setPalette(QPalette(QColor("#F0F0F0")));
    }

    //加载中文字符
    static void SetChinese()
    {
        QTranslator *translator = new QTranslator(qApp);
        translator->load(":/image/qt_zh_CN.qm");
        qApp->installTranslator(translator);
    }

    //判断是否是IP地址
    static bool IsIP(QString IP)
    {
        QRegExp RegExp("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
        return RegExp.exactMatch(IP);
    }
#if 0

    //显示信息框,仅确定按钮
    static void ShowMessageBoxInfo(QString info)
    {
        frmMessageBox *msg = new frmMessageBox;
        msg->SetMessage(info, 0);
        msg->exec();
    }

    //显示错误框,仅确定按钮
    static void ShowMessageBoxError(QString info)
    {
        frmMessageBox *msg = new frmMessageBox;
        msg->SetMessage(info, 2);
        msg->exec();
    }

    //显示询问框,确定和取消按钮
    static int ShowMessageBoxQuesion(QString info)
    {
        frmMessageBox *msg = new frmMessageBox;
        msg->SetMessage(info, 1);
        return msg->exec();
    }
#endif

    //延时
    static void Sleep(int sec)
    {
        QTime dieTime = QTime::currentTime().addMSecs(sec);
        while ( QTime::currentTime() < dieTime ) {
            QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
        }
    }

    //窗体居中显示
    static void FormInCenter(QWidget *frm)
    {
        int frmX = frm->width();
        int frmY = frm->height();
        QDesktopWidget w;
        int deskWidth = w.width();
        int deskHeight = w.height();
        QPoint movePoint(deskWidth / 2 - frmX / 2, deskHeight / 2 - frmY / 2);
        frm->move(movePoint);
    }
/*
    static bool init_object (void* object,QWidget* frm)
    {
        std::cout << "myhelp init object" << std::endl;
        Parameter* p = (Parameter*)object;
        get_widgegt_value (frm,p);
        p->get_value_from_widget (frm);

    }



    static void get_widgegt_value (QWidget* frm, void* object)
    {
        std:: cout << "todo" << std::endl;
    }

    static void input_setup_file (void* object)
    {
        Parameter* p = (Parameter*)object;
        s_setup_path = QApplication::applicationDirPath().toStdString() + "/MP2S.conf";
        //d_setup_file = path;
        std::cout << " myhelper path = " << s_setup_path << std::endl;
        QFile file (QString::fromStdString(s_setup_path));
        if (file.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            std::ofstream fbm (s_setup_path, std::ios::out);
            if (fbm.good()){
                fbm << *p;
                //std::cout << "path = " << path << std::endl;
                fbm.close();
            }
            //out << content << endl;

        }
    }


    static void call_process (
        QWidget* widget)
    {
        LINXIA::SolveUtil::get_instance()->solve_run ();
    }


    static std::string get_setup_path()
    {
        return s_setup_path;
    }


    static init_widget_value (std::list<QLineEdit*> line_edits)
    {
        std::vector <std::string>values;
        values.push_back("2013");
        values.push_back("7");
        values.push_back("1");
        values.push_back("0");
        values.push_back("0");
        values.push_back("0");

        values.push_back("2013");
        values.push_back("7");
        values.push_back("2");
        values.push_back("0");
        values.push_back("0");
        values.push_back("0");


        values.push_back("30");
        values.push_back("30");
        values.push_back("2.2");
        values.push_back("20");
        values.push_back("1000");
        values.push_back("1.5");

        values.push_back("150");
        values.push_back("150");
        values.push_back("3");
        values.push_back("1e-10");

        values.push_back("7078140");
        values.push_back("0");
        values.push_back("0");
        values.push_back("28.5");
        values.push_back("180");
        values.push_back ("180");


        for (std::list<QLineEdit*>::iterator i = line_edits.begin(); i != line_edits.end(); i++)
        {

            QLineEdit* edit = *i;
            int dis = std::distance(line_edits.begin(), i);
            std::string value = values[dis];
            edit->setText(QString::fromStdString(value));
        }

    }



    static init_all_widget_value (WidgetHolder* holder)
    {
        for (std::list<QCheckBox*>::iterator i = holder->check_boxes.begin(); i != holder->check_boxes.end(); i++)
        {

            QCheckBox* checkbox = *i;
            checkbox->setChecked(1);
        }
    }
    */

};


#endif // MYHELPER_H

